home *** CD-ROM | disk | FTP | other *** search
- Path: atglab.bls.com!Alun.Champion
- From: Alun.Champion@bridge.bst.bls.com (Alun Champion)
- Newsgroups: comp.lang.c
- Subject: Re: How to access memory allocated in function
- Date: 15 Jan 1996 21:44:45 GMT
- Organization: Computer People Inc.
- Message-ID: <ALUN.CHAMPION.96Jan15164445@g7240065.bridge.bst.bls.com>
- References: <4ddbe3$so3@josie.abo.fi> <4deg9p$bdi@nntpd2.cxo.dec.com>
- NNTP-Posting-Host: bstfirewall.bst.bls.com
- In-reply-to: Brian Hibbert's message of 15 Jan 1996 21:17:45 GMT
-
- In article <4deg9p$bdi@nntpd2.cxo.dec.com> Brian Hibbert <b_hibbert@csc32.enet.dec.com> writes:
- : csundqvi@abo.fi (Christoffer Sundqvist) wrote:
- :>
- :> How can i access memory allocated dynamically in a function after i leave
- :> the function,
-
- : int* sub() /* this version returns a pointer to the allocated memory */
- : {
- : return (int*) malloc(sizeof(int));
- ^^^^^^
- The cast is unnecessary in ANSI C and should be removed See FAQ section 7.7.
-
- : }
-
- : If you are using a C++ compiler, you can use a version that is closer to
- : your original code by using a reference to the main's pointer.
-
- : void sub (int& p)
- : {
- : p = (int*) malloc (sizeof(int));
- : }
-
- This is not really the approriate group for this - and its wrong.
- 'p' is just a reference to an int it needs to be a reference to a pointer
- to an int.
-
- void
- sub(int*& p)
- {
- p = (int*)malloc(sizeof(int));
- }
-
- And in C++ the cast is necessary. ;')
-
- Regards
-
- -A.
-
- --
- | A.Champion |
-